package com.lee.delegatedemo

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.widget.TextView
import kotlin.properties.ReadWriteProperty
import kotlin.reflect.KProperty


/**提供者委托**/
 operator fun TextView.provideDelegate(any: Any?,property:KProperty<*>) =
    object : ReadWriteProperty<Any?,String?> {
        override fun getValue(thisRef: Any?, property: KProperty<*>): String? = text as
                String

        override fun setValue(thisRef: Any?, property: KProperty<*>, value: String?) {
            text = value
        }

    }
/*** 以下代码 有错 ，不带？  textView会报错***/
//operator fun TextView.provideDelegate(any: Any,property:KProperty<*>) =
//    object : ReadWriteProperty<Any?,String?> {
//        override fun getValue(thisRef: Any, property: KProperty<*>): String? = text as
//                String
//
//        override fun setValue(thisRef: Any, property: KProperty<*>, value: String?) {
//            text = value
//        }
//
//    }
class MainActivity : AppCompatActivity() {




    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        var textView:TextView = findViewById(R.id.tv_hello)
        var message:String? by textView

        textView.text = "message_lze"
        println(message)
        Handler().postDelayed({
                              message = "lzw,data"
        },5000)

    }
}
